package org.ragdollcat.first.manager.disruptor;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * 消息生产者
 */
@Component
public class EditPictureEventProducer {

    @Resource
    private Disruptor<EditPictureDisruptorEvent> disruptor;

    public void publishEvent(EditPictureDisruptorEvent editPictureDisruptorEvent) {
        RingBuffer<EditPictureDisruptorEvent> ringBuffer = disruptor.getRingBuffer();
//获取下一个可用消息的槽位
        long next = ringBuffer.next();
        EditPictureDisruptorEvent event = ringBuffer.get(next);
        BeanUtils.copyProperties(editPictureDisruptorEvent,event);
        //发布事件
        ringBuffer.publish(next);

    }


    @PreDestroy
    public void shutdownGracefully(){
        disruptor.shutdown();
    }

}
